package com.track.mapping.content;

import com.track.*;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class Loader {
	
	private static Loader _LD;
	protected void onCreate()
	{
		if (_LD != null) return;
		_LD = this;
	}
	public static Loader getInstance()
	{
		return _LD;
	}
	/*
	public InputStream getURLStream() {
    	
   	 URL myFileUrl = null;    
   	 InputStream is = null;
   	 
   	 
        try {
             //myFileUrl= new URL("http://mt0.google.com/mt/v=w2.92&hl=en&x=604&y=768&z=11&s=Gali");
             myFileUrl= new URL(getGoogleTilePath());
        } catch (MalformedURLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }
        
        
        try {
       	 
            HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            int length = conn.getContentLength();
            is = conn.getInputStream();
             
            
        } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             is = null;
        }
   	
        return is;
   }
	
	*/
	public InputStream getURLStreamSet(int x, int y)
	{
	   	 URL myFileUrl = null;    
	   	 InputStream is = null;
	     try {
             //myFileUrl= new URL("http://mt0.google.com/mt/v=w2.92&hl=en&x=604&y=768&z=11&s=Gali");
             myFileUrl= new URL(getGoogleTilePathSet(x,y));
             Log.w("path", myFileUrl.toString());
        } catch (MalformedURLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }
        try {
       	 
            HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            int length = conn.getContentLength();
            is = conn.getInputStream();
             
            
        } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             
        }
   	
        return is;
		
	}
	
	protected String getGoogleTilePathSet(int x, int y ){
		
		double lat 	= mapview.currentMapLat;
		double lon 	= mapview.currentMapLon; 
		int z		= mapview.currentZoomLevel;
		
		return ("http://mt0.google.com/mt/v=w2.92&hl=en&x=" + (getTileXvalue(lat, z) + x)+ "&y=" + (getTileYvalue(lon, z) + y)+ "&z=" +z + "&s=Gali");
		
	}
	/*
	protected String getGoogleTilePath()
	{
		return ("http://mt0.google.com/mt/v=w2.92&hl=en&x=" + getTileXvalue(mapview.currentMapLon, mapview.currentZoomLevel) + "&y=" + getTileYvalue(mapview.currentMapLat, mapview.currentZoomLevel)+ "&z=" + mapview.currentZoomLevel + "&s=Gali");
	}
	*/
	/*
	public static void getTile() {
	   int zoom = 10;
	   double lat = 47.968056d;
	   double lon = 7.909167d;
	  // Log.w("Open Maps tile address : ", "http://tile.openstreetmap.org/" + getOpenTileNumber(lat, lon, zoom) + ".png");
	  // Log.w("Google Maps tile address : ", "http://mt0.google.com/mt/v=w2.92&hl=en&x=" + getTileXvalue(lon, zoom) + "&y=" + getTileYvalue(lat, zoom)+ "&z=" + zoom + "&s=Gali");
	   //http://mt0.google.com/mt/v=w2.92&hl=en&x=604&y=768&z=11&s=Gali
	 }
	/*
	 /**
	  * Basic set up to get a tile matched to latLong
	  * 
	  * @param lat - double latitute
	  * @param lon - double longtitude
	  * @param zoom - int zoom level
	  * @return
	  */
	/*
	 public static String getGoogleTileNumber(final double lat, final double lon, final int zoom) {
	   int xtile = (int)Math.floor( (lon + 180) / 360 * (1<<zoom) ) ;
	   int ytile = (int)Math.floor( (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * (1<<zoom) ) ;
	   return("" + zoom + "/" + xtile + "/" + ytile);
	  }
	 */

	 protected static int getTileXvalue(final double lon, final int zoom){
		 
		 return ((int)Math.floor( (lon + 180) / 360 * (1<<zoom) ));
	 }
	 
	 protected static int getTileYvalue(final double lat, final int zoom){
		 
		 return ((int)Math.floor( (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * (1<<zoom)));
	 }
	 
	 /*
	 public static String getOpenTileNumber(final double lat, final double lon, final int zoom) {
	   int xtile = (int)Math.floor( (lon + 180) / 360 * (1<<zoom) ) ;
	   int ytile = (int)Math.floor( (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * (1<<zoom) ) ;
	   return("" + zoom + "/" + xtile + "/" + ytile);
	  }
	*/ 
	
	
}
